/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-24 00:34:58
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:05:02
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.StudentExamRecordDao;
import com.xtedu.examproject.entity.StudentExamRecord;
import com.xtedu.examproject.entity.StudentExamRecordDTO;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentExamRecordDaoImpl implements StudentExamRecordDao {
    @Override
    public int insert(StudentExamRecord studentExamRecord) throws Exception {
        String sql = "insert into student_exam_records (stu_id,exam_id,class_id,theory_score,practice_score,exam_result,create_date) values (?,?,?,?,?,?,?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, studentExamRecord.getStudentId());
            ps.setInt(2, studentExamRecord.getExamId());
            ps.setInt(3, studentExamRecord.getClassId());
            ps.setDouble(4, studentExamRecord.getTheoryScore());
            ps.setDouble(5, studentExamRecord.getPracticeScore());
            ps.setBoolean(6, studentExamRecord.isExamResult());
            ps.setDate(7, studentExamRecord.getCreateDate());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(StudentExamRecord studentExamRecord) throws Exception {
        String sql = "update student_exam_records set stu_id=?,exam_id=?,class_id=?,theory_score=?,practice_score=?,exam_result=?,create_date=? where id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, studentExamRecord.getStudentId());
            ps.setInt(2, studentExamRecord.getExamId());
            ps.setInt(3, studentExamRecord.getClassId());
            ps.setDouble(4, studentExamRecord.getTheoryScore());
            ps.setDouble(5, studentExamRecord.getPracticeScore());
            ps.setBoolean(6, studentExamRecord.isExamResult());
            ps.setDate(7, studentExamRecord.getCreateDate());
            ps.setInt(8, studentExamRecord.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(StudentExamRecord studentExamRecord) throws Exception {
        String sql = "delete from student_exam_records where id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, studentExamRecord.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public StudentExamRecord findById(int id) throws Exception {
        String sql = "select * from student_exam_records where id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            StudentExamRecord studentExamRecord = new StudentExamRecord();
            while (rs.next()) {
                studentExamRecord.setId(rs.getInt("id"));
                studentExamRecord.setStudentId(rs.getInt("stu_id"));
                studentExamRecord.setExamId(rs.getInt("exam_id"));
                studentExamRecord.setClassId(rs.getInt("class_id"));
                studentExamRecord.setTheoryScore(rs.getFloat("theory_score"));
                studentExamRecord.setPracticeScore(rs.getFloat("practice_score"));
                studentExamRecord.setExamResult(rs.getBoolean("exam_result"));
                studentExamRecord.setCreateDate(rs.getDate("create_date"));
                studentExamRecord.setId(rs.getInt("id"));
            }
            return studentExamRecord;
        }
    }

    @Override
    public List<StudentExamRecord> findAll() throws Exception {
        String sql = "select * from student_exam_records";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<StudentExamRecord> studentExamRecords = new ArrayList<>();
            while (rs.next()) {
                StudentExamRecord studentExamRecord = new StudentExamRecord();
                studentExamRecord.setId(rs.getInt("id"));
                studentExamRecord.setStudentId(rs.getInt("stu_id"));
                studentExamRecord.setExamId(rs.getInt("exam_id"));
                studentExamRecord.setClassId(rs.getInt("class_id"));
                studentExamRecord.setTheoryScore(rs.getFloat("theory_score"));
                studentExamRecord.setPracticeScore(rs.getFloat("practice_score"));
                studentExamRecord.setExamResult(rs.getBoolean("exam_result"));
                studentExamRecord.setCreateDate(rs.getDate("create_date"));
                studentExamRecords.add(studentExamRecord);
            }
            return studentExamRecords;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }


}
